package me.zk.practice1.s;

/**
 * @author zhangkai
 * @date 2023/7/13 10:16
 */
public class LS_Offer_67 {
    public static int strToInt(String str) {
        char[] chars = str.trim().toCharArray();
        if (chars.length == 0) {
            return 0;
        }
        int sign=1;
        int value = 0;
        int index = 0;
        int bnry = Integer.MAX_VALUE/10;
        if (chars[0] == '-') {
            sign = -1;
            index = 1;
        } else if (chars[0] == '+') {
            index = 1;
        } else if (!Character.isDigit(chars[0])){
            return value;
        }
        for (int i = index; i < chars.length; i++) {
            if (Character.isDigit(chars[i])) {
                if (value > bnry || value == bnry && chars[i] > '7') {
                    return sign==1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                } else {
                    value = value*10 + (chars[i]-'0');
                }
            } else {
                break;
            }
        }
        return value * sign;
    }

    public static void main(String[] args) {
        System.out.println(strToInt("2147483648"));
    }
}
